﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECCIT;
using OgilvyOne.Golf.Model;
using System.Data;
using System.Data.SqlClient;
using ECCIT.Data;
using OgilvyOne.Golf.IDAL;
namespace OgilvyOne.Golf.DAL
{
    public class MyFansDAL:IDALfans
    {
      const string C_TabName = "GOLF_MY_Fans";
      const string C_COL_FansID = "FANSID";
      const string C_COL_FansUID = "FANSUID";
      const string C_COL_MyUID = "USERUID";
      const string C_COL_FansName = "FANSNAME";
      const string C_COL_FansProImage = "FANSPROIMAGE";
      const string C_COL_FansAvatar = "FANSAVATAR";
      const string C_COL_IsMyStar = "ISMYSTAR";
      const string C_COL_InserTime = "INSERTTIME";
      const string C_COL_IsBakUp = "ISBAKUP";
      const string InsertSql = "Insert into "
                              + C_TabName + "("
                              + C_COL_FansUID + ","
                              + C_COL_MyUID + ","
                              + C_COL_FansName + ","
                              + C_COL_FansProImage + ","
                              + C_COL_FansAvatar + ","
                              + C_COL_InserTime + ","
                              + C_COL_IsMyStar + ","
                              +C_COL_IsBakUp
                              + ")values("
                          + "@" + C_COL_FansUID + ","
                          + "@" + C_COL_MyUID + ","
                          + "@" + C_COL_FansName + ","
                          + "@" + C_COL_FansProImage + ","
                          + "@" + C_COL_FansAvatar + ","
                          + "@" + C_COL_InserTime + ","
                          + "@" + C_COL_IsMyStar + ","
                          + "@" + C_COL_IsBakUp
                              + ")";
      const string selectsql = " select * from " + C_TabName;
      const string delesql = "delete "
                           + C_TabName
                           + " where "
                           + C_COL_MyUID
                      + "=@" + C_COL_MyUID
                           + " and "
                           + C_COL_IsBakUp + "=1";
      const string upsql = " update "
                          + C_TabName
                          + " set "
                          + C_COL_IsBakUp
                        + "=1"
                        + " where "
                        + C_COL_IsBakUp + "<>1"
                        + " and "
                        + C_COL_MyUID
                        + "=@" + C_COL_MyUID;

      private  ResultInfo Save(MyFansModel myFans)
      {
          if (myFans == null){ throw new ArgumentNullException("myFans"); };
          ResultInfo result = new ResultInfo(false);
          try
              {
          if (myFans.FansID==null)
          {
              
                  int affectNum = 0;
                  string cmd = InsertSql
                              + "select @@identity";
                  IDataParameter[] parms = new SqlParameter[8];
                  parms[0] = new SqlParameter(C_COL_FansUID, SqlDbType.NVarChar, 50);           
                  parms[0].Value = myFans.FansUID;

                  parms[1] = new SqlParameter(C_COL_MyUID, SqlDbType.NVarChar);               
                  parms[1].Value = myFans.MyUID;

                  parms[2] = new SqlParameter(C_COL_FansName, SqlDbType.NVarChar, 50);
                  parms[2].Value = myFans.FansScreenName;

                  parms[3] = new SqlParameter(C_COL_FansProImage, SqlDbType.NVarChar, 50);
                  parms[3].Value = myFans.FansProImageUrl;

                  parms[4] = new SqlParameter(C_COL_FansAvatar, SqlDbType.NVarChar, 50);
                  parms[4].Value = myFans.FansAvatarLarge;

                  parms[5] = new SqlParameter(C_COL_InserTime, SqlDbType.NVarChar, 50);
                  myFans.InsertTime = DateTime.Now;
                  parms[5].Value = myFans.InsertTime;

                  parms[6] = new SqlParameter(C_COL_IsMyStar,SqlDbType.Int);
                
                  parms[6].Value = myFans.IsMyStar?1:0;

                  parms[7] = new SqlParameter(C_COL_IsBakUp,SqlDbType.Int);

                  parms[7].Value =myFans.IsBakup?1:0;
                  object o = DBHelper.ExecuteScalar(cmd, parms);
                  if (o != null && o != DBNull.Value)
                  {
                      myFans.FansID = Convert.ToInt32(o);
                      affectNum = 1;
                  }
                  if (affectNum > 0)
                  {
                      result.Message = "会员数据保存成功！";
                      result.Result = true;
                      result.ReturnObject = myFans;
                  }

                                      

              }
             

          }
          catch (Exception ex)
          {

              result.Exception = ex;
              result.Message = string.Format("保存会员数据时发生异常。异常信息：{0}", ex.Message);
          }
          return result;

      }
      public ResultInfo ADD(List<MyFansModel> myFansl)
      {
          if (myFansl.Count==0)
          {
            throw new ArgumentNullException("myFans"); 
          }
          deleteBak(myFansl[0].MyUID);
          update(myFansl[0].MyUID);
          foreach (MyFansModel myFans in myFansl)
          {

              Save(myFans);
          }
          
          return null;
      }
      private bool update(string myuid)
      {
          if (myuid == null
            || myuid.Trim().Length == 0)
          {
              throw new ArgumentNullException("myFans");
          }
          string sql = upsql;
          IDataParameter[] parms = new SqlParameter[1];
          parms[0] = new SqlParameter(C_COL_MyUID, SqlDbType.NVarChar, 50);
          parms[0].Value = myuid;
          int affertNum;
          affertNum = DBHelper.ExecuteNonQuery(sql, parms);
          return affertNum > 0 ? true : false;
      }
      private bool deleteBak(string myuid)
      {
          if (myuid==null
            ||myuid.Trim().Length==0)
          {
              throw new ArgumentNullException("myFans");
          }
          string sql = delesql;
          IDataParameter[] parms = new SqlParameter[1];
          parms[0] = new SqlParameter(C_COL_MyUID,SqlDbType.NVarChar,50);
          parms[0].Value = myuid;
          int affertNum;
          affertNum=DBHelper.ExecuteNonQuery(sql, parms);


          return affertNum > 0 ? true : false;
      }
      /// <summary>
      /// 查询
      /// </summary>
      /// <param name="par">分页项对象</param>
      /// <param name="myUID">session的UID</param>
      /// <param name="fansName">好友查询关键字</param>
      /// <returns></returns>
      public PagingDataSet GetMyFans(PagingParameter par, string myUID,string fansName,bool IsBakup)
      {

          if (myUID.Trim().Length == 0
           || myUID == null
           || par==null)
             
          {
              throw new ArgumentNullException("modelInfo");
          }
          string newsql=selectsql;
          DataSet ds = new DataSet();
          int arr=0;
          IDataParameter[] parms = new SqlParameter[3];
          
          
           newsql += " where "+C_COL_MyUID
                     + "=@" +C_COL_MyUID;
           parms[0] = new SqlParameter(C_COL_MyUID,SqlDbType.NVarChar,50);
           parms[0].Value = myUID;
           if (IsBakup)
           {
               newsql += " and " + C_COL_IsBakUp
                      + "=@" + C_COL_IsBakUp;
               parms[1] = new SqlParameter(C_COL_IsBakUp, SqlDbType.Int);
               parms[1].Value =1;
           }
           else
           {
               newsql += " and " + C_COL_IsBakUp
                     + "=@" + C_COL_IsBakUp;
               parms[1] = new SqlParameter(C_COL_IsBakUp, SqlDbType.Int);
               parms[1].Value = 0;
           }
           if (fansName != null && fansName.Trim() != "")
           {
               newsql += " and " + C_COL_FansName
                       + "  like '%" + fansName + "%' ";
               parms[2] = new SqlParameter(C_COL_FansName, fansName);
             //  parms[2] = new SqlParameter(C_COL_FansName, SqlDbType.NVarChar, 50);
              // parms[2].Value = fansName;
           }
           else
           {
               parms[2] = new SqlParameter(C_COL_FansName, SqlDbType.NVarChar, 50);
               parms[2].Value = "";
           }
              
              
          
          
          
              ds = DBHelper.ExecuteDataSet(newsql, parms, par.CurrentPageIndex, par.PageSize, out arr);
          
         
         if (ds.Tables[0].Rows.Count>0)
          {
                  return new PagingDataSet(ds, arr);
          }
              
        
          else
          {
              return null;
          }



          
      }

      


                              
      

    }
}
